package com.cqie.tes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqie.tes.comm.R;
import com.cqie.tes.comm.qo.BaseQo;
import com.cqie.tes.comm.service.impl.BaseServiceImpl;
import com.cqie.tes.domain.SysRole;
import com.cqie.tes.domain.qo.SysRoleQo;
import com.cqie.tes.domain.vo.SysRoleVo;
import com.cqie.tes.domain.vo.sys.role.SysRoleOptionVo;
import com.cqie.tes.mapper.SysRoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cqie.tes.service.SysRoleService;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysRoleServiceImpl extends BaseServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    SysRoleMapper sysRoleMapper;
    /**
     * 获取数据列表
     *
     * @param query 查询条件
     * @return
     */
    @Override
    public R getList(BaseQo query) {

        // 1
        SysRoleQo qo = (SysRoleQo) query;

        // 2
        QueryWrapper<SysRole> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(qo.getName())){
            qw.like("name",qo.getName());
        }
        qw.eq("mark", 1);
        qw.orderByDesc("id");
        // 3
        IPage<SysRole> page = new Page<>(qo.getPage(),qo.getLimit());
        IPage<SysRole> data = sysRoleMapper.selectPage(page,qw);
        List<SysRole> roleList = data.getRecords();

        // 4
        List<SysRoleVo> sysRoleVoList = new ArrayList<>();

        if (!roleList.isEmpty()) {
            roleList.forEach(item->{
                SysRoleVo sysRoleVo = new SysRoleVo();
                BeanUtils.copyProperties(item,sysRoleVo);

                // 设置vo字段

                //
                sysRoleVoList.add(sysRoleVo);
            });
        }
        Long total = data.getTotal();
        System.out.println(total);

        return R.ok().message("查询成功").data("total", total).data("rows", sysRoleVoList);
    }

    @Override
    public R edit(SysRole entity) {
        return super.edit(entity);
    }

    @Override
    public R deleteById(Integer id) {
        if (id == null || id == 0) {
            return R.error().message("记录ID不能为空");
        }
        SysRole entity = this.getById(id);
        if (entity == null) {
            return R.error().message("记录不存在");
        }
        return super.delete(entity);
    }

    @Override
    public R optionList() {

        // 结果列表
        List<SysRoleOptionVo> vos  =new ArrayList<>();
        // 一级菜单
        QueryWrapper<SysRole> qw = new QueryWrapper<>();
        qw.eq("mark", 1);
        qw.orderByAsc("sortCode");

        List<SysRole> entityList = sysRoleMapper.selectList(qw);

        entityList.forEach(entity->{
            SysRoleOptionVo vo = new SysRoleOptionVo();
            BeanUtils.copyProperties(entity,vo);

            vos.add(vo);
        });
        return R.ok().message("查询成功").data("options", vos);
    }
}
